package org.tag.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;

/**
 * Created by helinchong on 2017/5/15.
 */
public class SkipPageOrEvalPageTag extends TagSupport{
    @Override
    public int doEndTag() throws JspException {
        HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
        //获取来访问者地址,只有通过链接访问当前页的时候才能获取上一页的地地址
        String referer = request.getHeader("referer");
        //获取服务器名称 localhost 或 ip  getServerPort获取端口
        String url =  "http://"+request.getServerName();
        if(referer!=null && referer.startsWith(url)){
            //不继续执行之后的代码
            return EVAL_PAGE;
        }else{
            try {
                pageContext.getOut().print("Can not access");
            } catch (IOException e) {
                e.printStackTrace();
            }
            //不继续执行之后的代
            return SKIP_PAGE;
        }
    }
}
